package com.myproject.shop.controller;


import com.myproject.shop.domain.Staff;
import com.myproject.shop.service.IStaffService;
import com.myproject.shop.util.AjaxResult;
import com.myproject.shop.util.BizException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
    @RequestMapping("/api/staff")
    @Api(tags = "员工管理825王慧敏")
    public class StaffController {
        @Autowired
        private IStaffService staffService;

        @ApiOperation("获得全部员工列表825王慧敏")
        @GetMapping("/listAll")
        public AjaxResult getAllStaffList() {
            return AjaxResult.success(staffService.selectAllStaff());
        }

        @ApiOperation("根据name查询员工列表825王慧敏")
        @ApiImplicitParam(name = "name", value = "员工姓名",
                dataType = "string", dataTypeClass = String.class)
        @GetMapping("/list")
        public AjaxResult list(@RequestParam(value = "name", required = false) String staffName) {
            List<Staff> list = staffService.selectStaffByParam(staffName);
            return AjaxResult.success(list);
        }

        @ApiOperation("根据id获取员工详细信息825王慧敏")
        @ApiImplicitParam(name = "id", value = "员工编号（ID）",
                required = true, dataType = "integer", paramType = "path",
                dataTypeClass = Integer.class)
        @GetMapping("/{id}")
        public AjaxResult getInfo(@PathVariable("id") Integer sid) {
            Staff staff = staffService.selectStaffById(sid);
            if (staff == null || staff.getSid() == null) {
                throw new BizException(500, "员工 " + sid + " 不存在");
            }
            return AjaxResult.success(staff);
        }

        @ApiOperation("新增员工825王慧敏")
        @PostMapping
        public AjaxResult add(@Validated @RequestBody Staff staff) {
            return AjaxResult.success(staffService.insertStaff(staff));
        }

        @ApiOperation("修改员工825王慧敏")
        @PutMapping
        public AjaxResult edit(@RequestBody Staff staff) {
            return AjaxResult.success(staffService.
                    updateStaff(staff));
        }

        @ApiOperation("删除员工825王慧敏")
        @ApiImplicitParam(name = "sid", value = "员工编号（ID）", required = true, dataType = "integer",
                paramType = "path", dataTypeClass = Integer.class)
        @DeleteMapping("/{sid}")
        public AjaxResult remove(@PathVariable Integer sid) {
            return AjaxResult.success(staffService.deleteStaffById(sid));
        }

}
